package ControllerStarter;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import Controllers.PawnController;
import Controllers.ChessPieceControllerInterface;
import TechStuff.RmiStarter;


public class PawnControllerStarter extends RmiStarter  {

	public PawnControllerStarter()  {
		super(ChessPieceControllerInterface.class);
		doCustomRmiHandling();
	}

	@Override
	public void doCustomRmiHandling() {
		try {
			ChessPieceControllerInterface obj = new PawnController();
			ChessPieceControllerInterface stub = (ChessPieceControllerInterface) UnicastRemoteObject.exportObject(obj, 0);

		    // Bind the remote object's stub in the registry
		    Registry registry = LocateRegistry.getRegistry();
		    registry.bind(ChessPieceControllerInterface.name, stub);

		    System.err.println("PawnControllerStarter ready");
		} catch (Exception e) {
		    System.err.println("PawnControllerStarter exception: " + e.toString());
		    e.printStackTrace();
		}		
	}
	
	public String createChessPiece(int index)
	{
		try {
			ChessPieceControllerInterface obj = new PawnController();
			ChessPieceControllerInterface stub = (ChessPieceControllerInterface) UnicastRemoteObject.exportObject(obj, 0);

		    // Bind the remote object's stub in the registry
		    Registry registry = LocateRegistry.getRegistry();
		    String name = ChessPieceControllerInterface.name + index;
		    registry.bind(name, stub);

		    System.err.println(name + " ready");
		    return name;
		} catch (Exception e) {
		    System.err.println("ChessPieceControllerInterface exception: " + e.toString());
		    e.printStackTrace();
		    return null;
		}
	}
}
